/**
 * 插件入口文件，用以插件的初始化；需与package.json中的command字段保持一致
 */
import "./register-alias";
import * as vscode from "vscode";
import { DoradoViewEditor } from "@/dorado/ide/ViewEditor";
import { DoradoViewCreator } from "@/dorado/ide/ViewCreator";
import { SideBarWebviewProvider } from "@/dorado/tools/SideBarWebview";

export function activate(context: vscode.ExtensionContext) {
  // 功能1，注册工具箱到侧边栏sideBar
  context.subscriptions.push(
    vscode.window.registerWebviewViewProvider(
      "dorado.tools.webview",
      new SideBarWebviewProvider(context)
    )
  );
  console.log("Dorado-tools initializing success............");

  // 功能2，注册Dorado视图编辑器到vscode命令到资源管理器窗口中
  const maps: Map<string, vscode.WebviewPanel> = new Map(); //全局存储webviewPanel
  let disposable = vscode.commands.registerCommand(
    "extension.openWithDoradoAction",
    (uri: vscode.Uri) => {
      try {
        if (uri && uri.fsPath) {
          const doradoIde = new DoradoViewEditor(context);
          doradoIde.init(uri, maps);
        } else {
          vscode.window.showErrorMessage("无效的文件路径");
        }
      } catch (error) {
        console.error("Error initializing Dorado IDE:", error);
        vscode.window.showErrorMessage(
          "初始化Dorado视图编辑器失败，请检查日志。"
        );
      }
    }
  );
  context.subscriptions.push(disposable);

  // 功能3，注册创建Dorado视图文件到vscode命令到资源管理器窗口中
  let disposable2 = vscode.commands.registerCommand(
    "extension.createViewXmlAction",
    async (uri: vscode.Uri) => {
      const viewCreator = new DoradoViewCreator();
      await viewCreator.createViewFile(uri);
    }
  );
  context.subscriptions.push(disposable2);
  console.log("Dorado IDE initializing success............");

  // 其它功能...
}

export function deactivate() {}
